package com.qingfeng.medical.controller.front.info;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.base.ro.EnumsRo;
import com.qingfeng.medical.biz.information.domain.entity.Dict;
import com.qingfeng.medical.biz.information.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 数据字典表
 *
 * @author 清风学Java
 * @date 2023-04-08 23:43:31
 */
@Api(tags = "数据字典表管理")
@RestController
@RequestMapping("/api/info/dict")
@Validated
public class DictApiController extends BaseController {

    @Autowired
    private DictService dictService;

    @ApiOperation("根据数据Id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id){
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    @ApiOperation(value = "查询证件类型", notes = "查询证件类型")
    @GetMapping("/certificates")
    @Log(title = "数据字典管理", businessType = BusinessType.SELECT, message = "查询证件类型")
    public Result<List<EnumsRo>> certificatesType() {
        Dict dict = dictService.getOne(
                new LambdaQueryWrapper<Dict>()
                        .eq(Dict::getDictCode, "CERTIFICATESTYPE")
        );
        return Result.ok(
                dictService.list(
                                new LambdaQueryWrapper<Dict>()
                                        .eq(Dict::getParentId, dict.getId())
                        ).stream()
                        .map(d -> EnumsRo.builder()
                                .label(d.getDictName())
                                .value(d.getDictName())
                                .build()
                        )
                        .collect(Collectors.toList())
        );
    }

}
